//
//  LyMapViewController.m
//  MailList
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 KPY. All rights reserved.
//

#import "LyMapViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface LyMapViewController ()<BMKGeoCodeSearchDelegate, BMKMapViewDelegate, BMKLocationServiceDelegate> {
    
    BMKMapView *_mapView;
    BMKGeoCodeSearch *_searcher;
    BMKLocationService *_locService;
    
    CLLocationCoordinate2D coor;
    NSString *markAddress;
    
}


@end




@implementation LyMapViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    
//    BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
//    self.view = mapView;
    
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:_mapView];
    
    
    //初始化检索对象
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
//    geoCodeSearchOption.city= [self.temp substringToIndex:2];
    geoCodeSearchOption.address = self.temp;
    NSLog(@"%@ %@", geoCodeSearchOption.city, geoCodeSearchOption.address);
    
    BOOL flag = [_searcher geoCode:geoCodeSearchOption];
    if(flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }
    
}

#pragma mark - GeoCodeSearch
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    
    // 地理编码功能
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        // 设置变量
        coor = result.location;
        markAddress = result.address;
        
        NSLog(@"%.6f,%.6f", coor.latitude, coor.longitude);
        
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
    
#if 0
    // 定位功能
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    
#endif
    
    // 添加PointAnnotation功能
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    // 经纬度设置
    CLLocationCoordinate2D coorItem;
    coorItem.latitude = coor.latitude;
    coorItem.longitude = coor.longitude;
    // 大头针设置
    annotation.coordinate = coorItem;
    annotation.title = markAddress;
    
    [_mapView addAnnotation:annotation];
    
    // 默认显示大头针标题
    [_mapView selectAnnotation:annotation animated:YES];
    
    // 地图显示当前定位位置
    [_mapView setCenterCoordinate:coor animated:YES];
}

#pragma mark - annoation
// Override
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
